package com.company.biz.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.biz.user.mapper.ClientDao;
import com.company.biz.user.service.ClientService;
import com.company.biz.user.mapper.UserDao;
import com.company.common.enums.TableNamePrefixEnum;
import com.company.common.util.DirectFieldFiller;
import com.company.common.util.IdentifierGeneratorUtil;
import com.company.user.entity.ClientEntity;
import com.company.user.req.AddClientReq;
import com.company.user.req.ClientListPageReq;
import com.company.user.resp.ClientInfoResp;
import com.company.user.entity.UserEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ClientServiceImpl extends ServiceImpl<ClientDao, ClientEntity> implements ClientService {

    @Resource
    private UserDao userDao;

    @Override
    public Page<ClientInfoResp> getClientListPage(ClientListPageReq req) {
        LambdaQueryWrapper<ClientEntity> clientEntityQueryWrapper = new QueryWrapper<ClientEntity>().lambda();
        clientEntityQueryWrapper.like(StringUtils.isNotBlank(req.getClientName()), ClientEntity::getClientName, req.getClientName());
        clientEntityQueryWrapper.eq(ClientEntity::getIsDel,"n");
        Page<ClientEntity> page = page(new Page<ClientEntity>(req.getPage(), req.getSize()), clientEntityQueryWrapper);
        List<ClientInfoResp> clientInfoResps = new ArrayList<>();
        for (ClientEntity record : page.getRecords()) {
            ClientInfoResp clientInfoResp = new ClientInfoResp();
            record.copyTo(clientInfoResp);
            clientInfoResps.add(clientInfoResp);
        }
        //补充操作人的姓名
        supplementUserName(clientInfoResps);
        Page<ClientInfoResp> clientInfoRespPage = new Page<>();
        clientInfoRespPage.setRecords(clientInfoResps);
        clientInfoRespPage.setTotal(page.getTotal());
        clientInfoRespPage.setSize(page.getSize());
        clientInfoRespPage.setPages(page.getPages());
        clientInfoRespPage.setCurrent(page.getCurrent());
        return clientInfoRespPage;
    }

    @Override
    public boolean addClient(AddClientReq req, UserEntity userEntity) {
        ClientEntity clientEntity = new ClientEntity();
        req.copyTo(clientEntity);
        clientEntity.setClientId(IdentifierGeneratorUtil.getOwnId(TableNamePrefixEnum.TBCUSTOMER));
        clientEntity.setUserId(userEntity.getUserId());
        clientEntity.setCreateTime(new Date());
        return save(clientEntity);
    }

    @Override
    public ClientInfoResp getClientByClientId(String clientId) {
        ClientEntity clientEntity = getById(clientId);
        ClientInfoResp clientInfoResp = new ClientInfoResp();
        clientEntity.copyTo(clientInfoResp);
        UserEntity userEntity = userDao.selectById(clientEntity.getUserId());
        clientInfoResp.setUserName(userEntity.getUserName());
        return clientInfoResp;
    }

    @Override
    public boolean updateClientByClientId(ClientInfoResp resp) {
        ClientEntity clientEntity = new ClientEntity();
        resp.copyTo(clientEntity);
        return updateById(clientEntity);
    }

    @Override
    public boolean deleteClientByClientId(List<String> clientIds) {
        return update(new UpdateWrapper<ClientEntity>().lambda()
                .set(ClientEntity::getIsDel, "y")
                .in(ClientEntity::getClientId, clientIds));
    }


    //补充操作人的姓名
    public void supplementUserName(List<ClientInfoResp> clientInfoResps) {
        //补充部门名
        DirectFieldFiller<String, UserEntity, ClientInfoResp> UserNameFiller = new DirectFieldFiller<String, UserEntity, ClientInfoResp>() {
            @Override
            protected String whichIdNeed(ClientInfoResp item) {
                return item.getUserId();
            }

            @Override
            public String getIdFromValue(UserEntity obj) {
                return obj.getUserId();
            }

            @Override
            public List<UserEntity> loadFromDB(List<String> ids) {
                return userDao.selectList(new QueryWrapper<UserEntity>().lambda().in(UserEntity::getUserId, ids));
            }

            @Override
            public void fillField(ClientInfoResp obj, UserEntity val) {
                obj.setUserName(val.getUserName());
            }
        };
        UserNameFiller.fill(clientInfoResps);
    }

}


